﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DownloadBook {
    public class HttpGet {
        public static string httpGet(string url) {
            return httpGet(url, null);
        }

        public static string httpGet(string url, string locationUrl) {
            string text = "";
            if (url != null && !"".Equals(url)) {
                Stream stream = null;
                try {
                    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                    if (locationUrl != null && !"".Equals(locationUrl)) {
                        httpWebRequest.Referer = locationUrl;
                    }

                    //设置User-Agent 用于支持 HTTPS
                    httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

                    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    string contentType = httpWebResponse.ContentType;
                    stream = httpWebResponse.GetResponseStream();
                    byte[] array = new byte[8192];
                    MemoryStream memoryStream = new MemoryStream();
                    long contentLength = httpWebResponse.ContentLength;
                    long num = 0L;
                    int num2;
                    do {
                        num2 = stream.Read(array, 0, array.Length);
                        memoryStream.Write(array, 0, num2);
                        num += (long)num2;
                    }
                    while (num2 >= 1 && (contentLength <= 0L || num < contentLength));
                    string text2 = httpWebResponse.ContentEncoding;
                    if (text2 != null && !"".Equals(text2)) {
                        text2 = text2.ToLower();
                        MemoryStream memoryStream2 = memoryStream;
                        memoryStream2.Position = 0L;
                        memoryStream = new MemoryStream();
                        bool flag = false;
                        if (text2.IndexOf("gzip") > -1) {
                            stream.Close();
                            stream = new GZipStream(memoryStream2, CompressionMode.Decompress);
                            flag = true;
                        } else if (text2.IndexOf("deflate") > -1) {
                            stream.Close();
                            stream = new DeflateStream(memoryStream2, CompressionMode.Decompress);
                            flag = true;
                        }
                        if (flag) {
                            num = 0L;
                            do {
                                num2 = stream.Read(array, 0, array.Length);
                                memoryStream.Write(array, 0, num2);
                                num += (long)num2;
                            }
                            while (num2 >= 1);
                        }
                    }
                    string text3 = "";
                    byte[] bytes = memoryStream.ToArray();
                    memoryStream.Close();
                    text = Encoding.Default.GetString(bytes);
                    if (contentType != null) {
                        string pattern = "charset=([\\w\\W]*?)$";
                        Regex regex = new Regex(pattern);
                        Match match = regex.Match(contentType);
                        text3 = match.Groups[1].ToString();
                    }
                    if ("".Equals(text3)) {
                        string pattern2 = "charset[\\s]{0,}=[^>]*?>";
                        Regex regex = new Regex(pattern2);
                        Match match = regex.Match(text);
                        text3 = match.Groups[0].ToString();
                    }
                    if (!"".Equals(text3)) {
                        text3 = text3.ToLower();
                        if (text3.IndexOf("utf") > -1) {
                            text = Encoding.UTF8.GetString(bytes);
                        }
                    }
                } catch (Exception e) {
                    Console.WriteLine(string.Format("出错了：{0}，URL：{1}" , e.Message, url));
                }
                if (stream != null) {
                    stream.Close();
                }
            }
            return text;
        }
    }
}
